package com.control;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Callable;
import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.dao.DemoDao;

import hyl.core.MyFun;
import hyl.core.run.MyRun;

@Controller
public class DemoControl {

	@Autowired
	DemoDao daodemo;

	@RequestMapping("/demoform1.do")
	public String demoform1(String userid, Model model) {
		System.out.println(userid);
		return "login";
	}

	@RequestMapping("/demoform2.do")
	public String demoform2(Model model, HttpServletRequest req) {
		Object[] objs = new Object[4];
		objs[0] = req.getParameter("userid");
		objs[1] = req.getParameter("name");
		objs[2] = req.getParameter("contact");
		objs[3] = req.getParameter("pass");
		// ???未测试
		Long id = MyRun.getInstance("test", 1).call(new Callable<Long>() {
			@Override
			public Long call() throws Exception {
				return daodemo.insertUser(objs);
			}
		});

		return "edit.jsp";

	}

	@SuppressWarnings("rawtypes")
	@RequestMapping("/demolist1.do")
	@ResponseBody
	public List<List<Object>> demolist1(HttpServletRequest req, String rs_index, String rs_size) {
		Map<String, Object> objs = new HashMap<String, Object>();
		objs.put("rs_index", MyFun.str2int(rs_index));
		objs.put("rs_size", MyFun.str2int(rs_size));
		objs.put("where", req.getParameter("where"));
		List<List<Object>> list = daodemo.list(objs);
		return list;
	}

	@RequestMapping("/demolist3.do")
	public String demolist3(HttpServletRequest req) {
		// 如果需要查询参数 自己加
		long cnt = daodemo.count(null);
		List<Map<String, Object>> list = daodemo.select(null);
		req.setAttribute("count", cnt);
		req.setAttribute("list", list);
		return "table.jsp";
	}

	@RequestMapping("/demomq1.do")
	// localhost:8080/hjsp/demomq1.do?rs_index=1&rs_size=10
	@ResponseBody
	public String demolist2(HttpServletRequest req, String rs_index, String rs_size) {
		Map<String, String> objs = new HashMap<String, String>();
		objs.put("rs_index", rs_index);
		objs.put("rs_size", rs_size);
		objs.put("where", req.getParameter("where"));

		return "ok";
	}

}
